<?php
/**
 * Created by PhpStorm.
 * User: Chris
 * Date: 14/12/22
 * Time: 下午4:20
 * php oil r weixincron:refreshToken
 */


namespace Fuel\Tasks;
use \Fuel\Core\Config;
use \Fuel\Core\Package;

// must be load the package at first , because we've extends the \Wolfteam\Task\BaseTask
Package::load('wolfteam');

class WeixinCron extends \Wolfteam\Task\BaseTask {



    protected static $weixinService;

    protected static function weixinServiceInstance(){
        if (self::$weixinService){
            return self::$weixinService;
        }

        \Package::load('weixin');
        self::$weixinService = new \Koodev\WeixinService();
        return self::$weixinService ;
    }

    /**
     * 刷新 token
     * php oil r weixincron:refreshToken
     */
    public function refreshToken(){
        self::log( "RefreshToken..");
//        $accessToken  =\Model_SiteMeta::findMetaValue(\Model_SiteMeta::META_WEIXIN_ACCESS_TOKEN);
        $expireTime = \Wolfteam\Model\SiteMeta::findMetaValue(META_WEIXIN_ACCESS_TOKEN_REFRESH_TIME);
        $needRefresh = false;
        if ($expireTime==null){
            $needRefresh = true;
        }else{
            $interval = $expireTime-time();
            self::log( "Refresh expire interval=".$interval);
            if ($interval<=120){
                // 2 分钟内过期
                $needRefresh = true;
                self::log("Token will be expire in ".$interval." seconds, need refresh now!");
            }
        }

        if ($needRefresh){
            $weixinService = self::weixinServiceInstance();
            $response = $weixinService->requestServerAccessToken();

            self::log( "Renew AccessToken=". $response->access_token);
            \Wolfteam\Model\SiteMeta::updateMeta(META_WEIXIN_ACCESS_TOKEN, $response->access_token);
            \Wolfteam\Model\SiteMeta::updateMeta(META_WEIXIN_ACCESS_TOKEN_REFRESH_TIME, intval($response->expires_in)+time() );
        }else{
            self::log("Not need to refresh weixin token.");
        }
    }

    /**
     * @todo: need to implements getTicket
     * 刷新 jsticket
     * php oil r weixincron:refreshJsticket
     */
    public function refreshJsTicket(){
        self::log( "RefreshToken..") ;
        $expireTime = \Wolfteam\Model\SiteMeta::findMetaValue(META_WEIXIN_JS_TICKET_REFRESH_TIME);
        $needRefresh = false;
        if ($expireTime==null){
            $needRefresh = true;
        }else{
            $interval = $expireTime-time();
            self::log( "Refresh expire interval=".$interval);
            if ($interval<=120){
                // 2 分钟内过期
                $needRefresh = true;
                self::log("Token will be expire in ".$interval." seconds, need refresh now!");
            }
        }

        if ($needRefresh){
            $accessToken  = \Wolfteam\Model\SiteMeta::findMetaValue(META_WEIXIN_ACCESS_TOKEN);
            if ($accessToken==null){
                throw new \Exception("cannot find access token , your must get the access token at first!!!");
            }
            $weixinService = self::weixinServiceInstance();
            $response = $weixinService->getTicket($accessToken);

            self::log( "Renew AccessToken=". $response->ticket);
            \Wolfteam\Model\SiteMeta::updateMeta(META_WEIXIN_JS_TICKET, $response->ticket);
            \Wolfteam\Model\SiteMeta::updateMeta(META_WEIXIN_JS_TICKET_REFRESH_TIME, intval($response->expires_in)+time() );
        }else{
            self::log( "Not need to refresh weixin token.");
        }
    }


}